package com.cloudbroker.bcs.common.mq.rabbitmq.test;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.QueueingConsumer;

public class Recv {
    
    public static void main(String[] args) throws Exception {
        String host = "10.253.6.75";
        String port = "28151";
        String username = "pay";
        String password = "pay";
        String virtualHost = "pay";
        String queueName = "bccs.pay.payresult.coa";
        ConnectionFactory factory = null;
        if (args.length >= 5) {
            host = args[0];
            port = args[1];
            username = args[2];
            password = args[3];
            virtualHost = args[4];
            queueName = args[5];
        }
        factory = new ConnectionFactory();
        factory.setHost(host);
        factory.setPort(Integer.parseInt(port));
        factory.setUsername(username);
        factory.setPassword(password);
        factory.setVirtualHost(virtualHost);
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();
        
        channel.queueDeclare(queueName, false, false, false, null);
        System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
        
        QueueingConsumer consumer = new QueueingConsumer(channel);
        channel.basicConsume(queueName, true, consumer);
        
        while (true) {
            QueueingConsumer.Delivery delivery = consumer.nextDelivery();
            String message = new String(delivery.getBody());
            System.out.println(" [x] Received '" + message + "'");
        }
    }
    
}
